
package com.example.demodb.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

import java.util.List;
import java.util.Set;

@Entity
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(value = { "hibernateLazyInitializer"})//解决此问题：No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create
@Table(name = "user")
public class User {
    @Id
    @Column(nullable = false)//加了@column后,就会在数据库中自动生成  user_id 这种命名格式的键
    private String id;

    @Column(nullable = false)
    private String name;

    @Column
    private String password="default";

    @Column
    private String userImg="https://photo.colorhub.me/dSlX9AxjN-g/rs:auto:0:500:0/g:ce/fn:colorhub/bG9jYWw6Ly8vZjQvZjYvZTE3NWI1YjRmYmEwMmVhNjM5ZWFkMmRkMzhiMmJmYTZiZGMwZjRmNi5qcGc.webp";


    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnoreProperties("user")
    private Set<UserSong> userSongs;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnoreProperties("user")
    private Set<UserLove> userLoves;

    public UserSong addUserSong(Song song, Integer listenCount){
        UserSong userSong = song.findUserSong(this);
        if(userSong != null){
            return userSong;
        }
        userSong = new UserSong();
        userSong.setUser(this);
        userSong.setSong(song);
        userSong.setListenCount(listenCount);
        userSongs.add(userSong);
        return userSong;
    }

    public UserLove addUserLove(Song song) {
        UserLove userLove = song.findUserLove(this);
        if(userLove != null){
            return userLove;
        }
        userLove = new UserLove();
        userLove.setUser(this);
        userLove.setSong(song);
        userLoves.add(userLove);
        return userLove;
    }

    public UserSong changeListenCount(UserSong userSong, Long updateTime) {
        userSong.setListenCount(userSong.getListenCount()+1);
        userSong.setUpdateTime(updateTime);
        return userSong;
    }

    public UserSong changeUserSong(UserSong userSong, Song song, Long updateTime) {
        userSong.setUser(this);
        userSong.setSong(song);
        userSong.setListenCount(1);
        userSong.setUpdateTime(updateTime);
        return userSong;
    }


}
